﻿(function (module) {

    var ctrl = function ($log, $parse, $scope, $timeout, $rootScope) {
        var tags = [
            'Why not...',
            'Software Mason',
            'WPF',
            'Architecture',
            'Radical',
            'RavenDB',
            'TechEd',
            'Visual Studio',
            'Model View ViewModel',
            'Team Foundation Server',
            'Community',
            'Inversion of Control',
            'Domain Driven Design',
            'UI Composition',
            'Design Time Data',
            'NHibernate',
            'NServiceBus',
            'Wcf',
            'Xaml',
            'AOP',
            'Unit Tests',
            'Agile',
            'Attached Behaviors',
            'Extensibility',
            'PostSharp',
            'Silverlight',
            'WinRT',
            'tales of an outlier',
            'Attached Properties',
            'Azure',
            'Blendability',
            'CQRS',
            'Fluent Interfaces',
            'Identity',
            'Castle Windsor',
            'EmitMapper',
            'Metro',
            'Windows 8',
            'Windows Phone 7',
            'MEF',
            'Presentation Pattern',
            'Jason',
            'Lean',
            'Memento',
            'Security',
            'User Experience',
            'Ensure',
            'MVP Award',
            'SOA',
            'TDD',
            'VSIX',
            'Guisa',
            'Kanban',
            'MongoDB',
            'OpenXml',
            'Windows 7',
            'Windows Developer Conference',
            'async/await',
            'ACS',
            'Event Sourcing',
            'Italian Agile Day',
            'WPC/TechDays',
            'WebAPI'];

        $scope.taxonomies = [];
        $scope.searchResults = [];
        $scope.searchTerm = '';
        $scope.tagNameGetter = function (tag) {
            $log.debug('custom tagNameGetter:', tag);
            return tag;
        };
        $scope.executeSearch = function (term) {
            var results = _.filter(tags, function (t) {
                return t.toLowerCase().indexOf(term.toLowerCase()) === 0;
            });

            var promise = $timeout(function () {
                $rootScope.safeApply($scope, function () {
                    $log.debug('results:', results);
                    $scope.searchResults = results;
                });
            }, 300);

            return promise;
        };
        $scope.selectItem = function (item) {
            $log.debug('select invoked on: ', item);
            if (item !== null) {

                var existing = _.filter($scope.taxonomies, function (t) {
                    return t.toLowerCase() === item.toLowerCase();
                });

                $log.debug('existing: ', existing);

                if (existing.length === 0) {
                    $scope.taxonomies.push(item);
                }

                $scope.searchTerm = '';
            }
        };
    };
    ctrl.$inject = ['$log', '$parse', '$scope', '$timeout', '$rootScope'];
    module.controller('typeaheadWithTagsController', ctrl);

}(angular.module('topics.controllers')));